<%!

  Copyright (c) Facebook, Inc. and its affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

%><%^type:resolves_to_complex_return?%>
<% > types/semi_type%> <%service:name%>AsyncClient::sync_<%function:cpp_name%>(<% > service_common/function_param_list_const%>) {
  ::apache::thrift::RpcOptions rpcOptions;
<%#type:void?%>
  sync_<%function:cpp_name%>(rpcOptions<%function:comma%><% > service_common/param_list%>);
<%/type:void?%>
<%^type:void?%>
  return sync_<%function:cpp_name%>(rpcOptions<%function:comma%><% > service_common/param_list%>);
<%/type:void?%>
<%/type:resolves_to_complex_return?%>
<%#type:resolves_to_complex_return?%>
void <%service:name%>AsyncClient::sync_<%function:cpp_name%>(<% > types/type%>& _return<%function:comma%><% > service_common/function_param_list_const%>) {
  ::apache::thrift::RpcOptions rpcOptions;
  sync_<%function:cpp_name%>(rpcOptions, _return<%function:comma%><% > service_common/param_list%>);
<%/type:resolves_to_complex_return?%>
}

<%^type:resolves_to_complex_return?%>
<% > types/semi_type%> <%service:name%>AsyncClient::sync_<%function:cpp_name%>(apache::thrift::RpcOptions& rpcOptions<%function:comma%><% > service_common/function_param_list_const%>) {
<%/type:resolves_to_complex_return?%>
<%#type:resolves_to_complex_return?%>
void <%service:name%>AsyncClient::sync_<%function:cpp_name%>(apache::thrift::RpcOptions& rpcOptions, <% > types/type%>& _return<%function:comma%><% > service_common/function_param_list_const%>) {
<%/type:resolves_to_complex_return?%>
  apache::thrift::ClientReceiveState _returnState;
  apache::thrift::ClientSyncCallback<false> callback(&_returnState);
  auto protocolId = apache::thrift::GeneratedAsyncClient::getChannel()->getProtocolId();
  auto evb = apache::thrift::GeneratedAsyncClient::getChannel()->getEventBase();
  auto ctx = std::make_shared<apache::thrift::detail::ac::ClientRequestContext>(
      protocolId,
      rpcOptions.releaseWriteHeaders(),
      this->handlers_,
      this->getServiceName(),
      "<%service:name%>.<%function:name%>");
<%^function:returns_stream?%>
  auto wrappedCallback = apache::thrift::RequestClientCallback::Ptr(&callback);
<%/function:returns_stream?%><%#function:returns_stream?%>
  auto wrappedCallback = apache::thrift::createStreamClientCallback(
    apache::thrift::RequestClientCallback::Ptr(&callback),
    rpcOptions.getChunkBufferSize());
<%/function:returns_stream?%>
  <%function:cpp_name%>Impl(rpcOptions, ctx, std::move(wrappedCallback)<%function:comma%><% > service_common/param_list%>);
  callback.waitUntilDone(evb);
  _returnState.resetProtocolId(protocolId);
  _returnState.resetCtx(std::shared_ptr<apache::thrift::ContextStack>(ctx, &ctx->ctx));
  SCOPE_EXIT {
    if (_returnState.header() && !_returnState.header()->getHeaders().empty()) {
      rpcOptions.setReadHeaders(_returnState.header()->releaseHeaders());
    }
  };
  if (!_returnState.buf()) {
    assert(!!_returnState.exception());
    _returnState.exception().throw_exception();
  }
  return folly::fibers::runInMainContext([&] {
    <%^type:resolves_to_complex_return?%>
    <%#type:void?%>
      recv_<%function:cpp_name%>(_returnState);
    <%/type:void?%>
    <%^type:void?%>
      return recv_<%function:cpp_name%>(_returnState);
    <%/type:void?%>
    <%/type:resolves_to_complex_return?%>
    <%#type:resolves_to_complex_return?%>
      recv_<%function:cpp_name%>(_return, _returnState);
    <%/type:resolves_to_complex_return?%>
  });
}

<%#type:sync_methods_return_try?%>
<%^function:returns_sink?%>
folly::Try<apache::thrift::RpcResponseComplete<<% > types/semi_type%>>>
<%service:name%>AsyncClient::sync_complete_<%function:cpp_name%>(
    apache::thrift::RpcOptions& rpcOptions<%function:comma%> <% > service_common/function_param_list_const%>) {
  apache::thrift::ClientReceiveState returnState;
  apache::thrift::ClientSyncCallback<false> callback(&returnState);
  const auto protocolId = apache::thrift::GeneratedAsyncClient::getChannel()->getProtocolId();
  auto* const evb = apache::thrift::GeneratedAsyncClient::getChannel()->getEventBase();
  auto ctx = std::make_shared<apache::thrift::detail::ac::ClientRequestContext>(
      protocolId,
      rpcOptions.releaseWriteHeaders(),
      this->handlers_,
      this->getServiceName(),
      "<%service:name%>.<%function:name%>");
<%^function:returns_stream?%>
  auto wrappedCallback = apache::thrift::RequestClientCallback::Ptr(&callback);
<%/function:returns_stream?%><%#function:returns_stream?%>
  auto wrappedCallback = apache::thrift::createStreamClientCallback(
    apache::thrift::RequestClientCallback::Ptr(&callback),
    rpcOptions.getChunkBufferSize());
<%/function:returns_stream?%>
  <%function:cpp_name%>Impl(rpcOptions, ctx, std::move(wrappedCallback)<%function:comma%><% > service_common/param_list%>);

  callback.waitUntilDone(evb);
  returnState.resetProtocolId(protocolId);
  returnState.resetCtx(std::shared_ptr<apache::thrift::ContextStack>(ctx, &ctx->ctx));

  folly::Try<apache::thrift::RpcResponseComplete<<% > types/semi_type%>>> tryResponse;
  if (!returnState.buf()) {
    assert(returnState.isException());
  	tryResponse.emplaceException(std::move(returnState.exception()));
  } else {
    tryResponse.emplace();
    tryResponse->responseContext.rpcSizeStats = returnState.getRpcSizeStats();
    if (returnState.header() && !returnState.header()->getHeaders().empty()) {
  	  tryResponse->responseContext.headers = returnState.header()->releaseHeaders();
    }
    tryResponse->response = folly::makeTryWith([&] {
      return folly::fibers::runInMainContext([&] {
        <%^type:resolves_to_complex_return?%>
        return recv_<%function:cpp_name%>(returnState);
        <%/type:resolves_to_complex_return?%>
        <%#type:resolves_to_complex_return?%>
        <% > types/semi_type%> rv;
        recv_<%function:cpp_name%>(rv, returnState);
        return rv;
        <%/type:resolves_to_complex_return?%>
      });
    });
  }
  return tryResponse;
}
<%/function:returns_sink?%>
<%/type:sync_methods_return_try?%>
